package com.lonely.fx.property_change_support;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * @date: 2022-07-21 14:38
 * @author: ztkj-hzb
 * @description: jdk自带的--PropertyChangeSupport 类的学习使用
 **/
public class PropertyChangeSupportSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane anchorPane = new AnchorPane();

        HBox hBox = new HBox();
        hBox.setSpacing(20);

        Label labelWithName = new Label("请输入修改后的值");
        TextField textField = new TextField();
        Button button = new Button("确定");
        hBox.getChildren().addAll(labelWithName, textField, button);
        hBox.setStyle("-fx-background-color: pink");


        TextArea textArea = new TextArea();
        textArea.setPrefWidth(300);
        textArea.setPrefHeight(200);

        AnchorPane.setTopAnchor(textArea, 100.0);


        User user = new User();

        /**
         * 点击确定按钮
         */
        button.setOnAction(event -> {
            String newName = textField.getText();

            //重置user中的name属性，会触发监听器
            user.setName(newName);
            //user.setAge(Integer.valueOf(newName));
        });


        //配置监听器--监听所有属性的
        user.propertyChangeSupport.addPropertyChangeListener(evt -> {
            System.out.println("触发监听");
            textArea.setText(String.valueOf(evt.getNewValue()));
        });

        //只监听固定属性的
        //user.propertyChangeSupport.addPropertyChangeListener("name", evt -> textArea.setText(String.valueOf(evt.getNewValue())));


        anchorPane.getChildren().addAll(hBox, textArea);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();
    }
}
